/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import io.lettuce.core.dynamic.annotation.Param;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.*;
import me.zhengjie.modules.system.service.ClazzService;
import me.zhengjie.modules.system.domain.vo.ClazzQueryCriteria;
import lombok.RequiredArgsConstructor;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import me.zhengjie.modules.system.service.ClazzandstudentService;
import me.zhengjie.modules.system.service.CourseandclazzService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
* @author tianjiaqi
* @date 2024-04-06
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "班级管理")
@RequestMapping("/api/clazz")
public class ClazzController {

    private final ClazzService clazzService;

    private final CourseandclazzService courseandclazzService;

    private final ClazzandstudentService clazzandstudentService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('clazz:list')")
    public void exportClazz(HttpServletResponse response, ClazzQueryCriteria criteria) throws IOException {
        clazzService.download(clazzService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询班级")
    @ApiOperation("查询班级")
    @PreAuthorize("@el.check('clazz:list')")
    public ResponseEntity<PageResult<Clazz>> queryClazz(ClazzQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(clazzService.queryAll(criteria,page),HttpStatus.OK);
    }
    @GetMapping("/queryClazzMsg")
    @Log("查询班级详情")
    @ApiOperation("查询班级详情")
    public ResponseEntity<Clazz> queryClazzMsg(@Param("clazzId") Long clazzId){
       Clazz clazz = clazzService.getById(clazzId);
        List<Student>  studentList = clazzandstudentService.getClazzStudent(Math.toIntExact(clazzId));
        clazz.setCount((long) studentList.size());
        return new ResponseEntity<>(clazz,HttpStatus.OK);
    }

    @PostMapping
    @Log("新增班级")
    @ApiOperation("新增班级")
//    @PreAuthorize("@el.check('clazz:add')")
    public ResponseEntity<Object> createClazz(@Validated @RequestBody ClazzDo resources){

        Clazz clazz = new Clazz();
        clazz.setName(resources.getName());
        clazz.setId(resources.getId());
        clazz.setInfo(resources.getInfo());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 生成时间戳
        String timestamp = sdf.format(new Date());
        clazz.setCreatetime(timestamp);
        clazzService.create(clazz);



        Courseandclazz courseandclazz = new Courseandclazz();
        courseandclazz.setClazzId(clazz.getId());
        courseandclazz.setCourseId(resources.getCourseid());

        courseandclazzService.create(courseandclazz);

        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PutMapping
    @Log("修改班级")
    @ApiOperation("修改班级")
//    @PreAuthorize("@el.check('clazz:edit')")
    public ResponseEntity<Object> updateClazz(@Validated @RequestBody Clazz resources){
        clazzService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除班级")
    @ApiOperation("删除班级")
//    @PreAuthorize("@el.check('clazz:del')")
    public ResponseEntity<Object> deleteClazz(@RequestBody List<Long> ids) {

        clazzService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}